﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using ETM.Business.Handlers;
using ETM.Business.Models;
using Newtonsoft.Json;

namespace ETM.Web.Controllers
{
    public class TasksController : ApiController
    {
        public IEnumerable<Task> Get()
        {
            return new GetTask().All();
        }

        // GET api/tasks
        public IEnumerable<Task> Get(string status)
        {
            return new GetTask().ByStatus(status);
        }

        //// POST api/values
        //public void Post([FromBody]string taskName)
        //{
        //    new EditTask().AddOne(taskName, taskStatus);
        //}

        // POST api/values
        public Task Post([FromBody]string value)
        {
            if (!value.StartsWith("DELETE") && !value.StartsWith("UPDATE"))
            {
                var newTask = (Task)JsonConvert.DeserializeObject(value, typeof(Task));

                if (newTask.TaskId != "000")
                {
                    var createdTask = new EditTask().AddOne(newTask);
                    return createdTask;
                }
            }


            else if (value.StartsWith("DELETE"))
            {
                var taskId = value.Replace("DELETE", string.Empty);

                var deleteTask = new EditTask().DeleteOne(taskId);

            }

            else if (value.StartsWith("UPDATE"))
            {
                var taskId = value.Replace("UPDATE", string.Empty);

                var deleteTask = new EditTask().Finish(taskId);

            }
            return null;
        }

        // PUT api/values/5
        public void Put(int id, [FromBody]string value)
        {
        }

        // DELETE api/values/5
        public void Delete(int id)
        {
        }
    }
}
